//
//  FoundPasswordViewController.m
//  LoveAttention
//
//  Created by 鞠超 on 15/12/29.
//  Copyright © 2015年 JC. All rights reserved.
//

#import "FoundPasswordViewController.h"


#import "SVProgressHUD.h"

#import <SMS_SDK/SMSSDK.h>
//#import <SMS_SDK/CountryAndAreaCode.h>
@interface FoundPasswordViewController () <UITextFieldDelegate>
//电话
@property (nonatomic, strong) UITextField * text_tel;
//验证码
@property (nonatomic, strong) UITextField * text_captcha;
//密码
@property (nonatomic, strong) UITextField * text_password;
//再次密码
@property (nonatomic, strong) UITextField * text_nextpassword;

@end

@implementation FoundPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self p_navigationController];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"重置密码";
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
        btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
}
//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 布局
- (void)p_setupView
{
    //电话
    UIView * view_1 = [[UIView alloc] initWithFrame:CGRectMake(10, 64 + 10, self.view.frame.size.width - 20, 50)];
    view_1.backgroundColor = [UIColor whiteColor];
    view_1.layer.cornerRadius = 5;
    view_1.layer.borderColor = [UIColor grayColor].CGColor;
    view_1.layer.borderWidth = 1;
    [self.view addSubview:view_1];
    
    self.text_tel = [[UITextField alloc] initWithFrame:CGRectMake(15, 5, view_1.frame.size.width - 30, 40)];
    self.text_tel.placeholder = @"请填写手机号";
    self.text_tel.keyboardType = UIKeyboardTypeNumberPad;
    [view_1 addSubview:self.text_tel];
    
    //验证码
    UIView * view_2 = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(view_1.frame) + 10, 200, 50)];
    view_2.backgroundColor = [UIColor whiteColor];
    view_2.layer.cornerRadius = 5;
    view_2.layer.borderColor = [UIColor grayColor].CGColor;
    view_2.layer.borderWidth = 1;
    [self.view addSubview:view_2];
    
    self.text_captcha = [[UITextField alloc] initWithFrame:CGRectMake(15, 5, view_2.frame.size.width - 30, 40)];
    self.text_captcha.placeholder = @"请输入验证码";
    [view_2 addSubview:self.text_captcha];
    
    UIButton * btn_captcha = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_captcha.frame = CGRectMake(CGRectGetMaxX(view_2.frame) + 5, CGRectGetMaxY(view_1.frame) + 10, self.view.frame.size.width - CGRectGetMaxX(view_2.frame) - 15, 50);
    [btn_captcha setTitle:@"免费获取" forState:(UIControlStateNormal)];
    [btn_captcha setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_captcha.backgroundColor = [UIColor orangeColor];
    btn_captcha.layer.cornerRadius = 5;
    [self.view addSubview:btn_captcha];
    [btn_captcha addTarget:self action:@selector(btnbtn_captchaAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //密码
    UIView * view_3 = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(view_2.frame) + 10, self.view.frame.size.width - 20, 50)];
    view_3.backgroundColor = [UIColor whiteColor];
    view_3.layer.cornerRadius = 5;
    view_3.layer.borderColor = [UIColor grayColor].CGColor;
    view_3.layer.borderWidth = 1;
    [self.view addSubview:view_3];
    
    self.text_password = [[UITextField alloc] initWithFrame:CGRectMake(15, 5, view_1.frame.size.width - 30, 40)];
    self.text_password.placeholder = @"请输入新密码";
    self.text_password.secureTextEntry = YES;
    [view_3 addSubview:self.text_password];
    
    //再次密码
    UIView * view_4 = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(view_3.frame) + 10, self.view.frame.size.width - 20, 50)];
    view_4.backgroundColor = [UIColor whiteColor];
    view_4.layer.cornerRadius = 5;
    view_4.layer.borderColor = [UIColor grayColor].CGColor;
    view_4.layer.borderWidth = 1;
    [self.view addSubview:view_4];
    
    self.text_nextpassword = [[UITextField alloc] initWithFrame:CGRectMake(15, 5, view_1.frame.size.width - 30, 40)];
    self.text_nextpassword.placeholder = @"请再次输入新密码";
    self.text_nextpassword.secureTextEntry = YES;
    [view_4 addSubview:self.text_nextpassword];
    
    //btn重置密码
    UIButton * btn_change = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_change.frame = CGRectMake(20, CGRectGetMaxY(view_4.frame) + 20, self.view.frame.size.width - 40 , 40);
    [btn_change setTitle:@"重置密码" forState:(UIControlStateNormal)];
    [btn_change setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_change.layer.cornerRadius = 4;
    btn_change.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:btn_change];
    [btn_change addTarget:self action:@selector(btn_changeAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    self.text_password.delegate = self;
    self.text_tel.delegate = self;
    self.text_captcha.delegate = self;
    self.text_nextpassword.delegate = self;
}

#pragma mark - 点击获取验证码
- (void)btnbtn_captchaAction:(UIButton *)sender
{
//    NSLog(@"点击获取验证码");
    
    if (self.text_tel.text.length==11)
    {
        [SVProgressHUD showWithStatus:@"正在发送" maskType:SVProgressHUDMaskTypeBlack];
        
        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.text_tel.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
            if (!error)
            {
                [SVProgressHUD dismiss];
                sender.enabled=NO;
                sender.titleLabel.text=@"验证发送";
                
                [self.text_nextpassword resignFirstResponder];
                [self.text_password resignFirstResponder];
                [self.text_captcha resignFirstResponder];
                [self.text_tel resignFirstResponder];
                
            }
            else
            {
                [SVProgressHUD dismiss];
                UIAlertView* alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"codesenderrtitle", nil)
                                                              message:[NSString stringWithFormat:@"网络原因，验证码发送失败"]
                                                             delegate:self
                                                    cancelButtonTitle:NSLocalizedString(@"好的", nil)
                                                    otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
    }
    else
    {
        UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请正确填写手机号" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        [alert show];
    }

}

#pragma mark - 重置密码
- (void)btn_changeAction:(UIButton *)sender
{
    NSLog(@"点击完成重置密码");
    
    if(self.text_tel.text.length == 11 && self.text_captcha.text && [self.text_password.text isEqualToString:self.text_nextpassword.text])
    {
        
        
        [SMSSDK commitVerificationCode:self.text_captcha.text phoneNumber:self.text_tel.text zone:@"86" result:^(NSError *error) {
            if (!error)
            {
                NSLog(@"验证成功");
#warning +++++++在此加接口
                [SVProgressHUD showSuccessWithStatus:@"验证成功" maskType:SVProgressHUDMaskTypeBlack];
                
                [self.navigationController popViewControllerAnimated:YES];
            }
            else
            {
                NSLog(@"验证失败");
                [SVProgressHUD dismiss];
                NSString* str=[NSString stringWithFormat:NSLocalizedString(@"验证失败", nil)];
                UIAlertView* alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"通知", nil)
                                                              message:str
                                                             delegate:self
                                                    cancelButtonTitle:NSLocalizedString(@"确定", nil)
                                                    otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
//        [SMS_SDK commitVerifyCode:self.text_captcha.text result:^(enum SMS_ResponseState state) {
//            if (1 == state)
//            {
//                NSLog(@"验证成功");
//#warning +++++++在此加接口
//                [SVProgressHUD showSuccessWithStatus:@"注册成功" maskType:SVProgressHUDMaskTypeBlack];
//                
//                [self.navigationController popViewControllerAnimated:YES];
//            }
//            else if(0 == state)
//            {
//                NSLog(@"验证失败");
//                [SVProgressHUD dismiss];
//                NSString* str=[NSString stringWithFormat:NSLocalizedString(@"验证失败", nil)];
//                UIAlertView* alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"通知", nil)
//                                                              message:str
//                                                             delegate:self
//                                                    cancelButtonTitle:NSLocalizedString(@"确定", nil)
//                                                    otherButtonTitles:nil, nil];
//                [alert show];
//            }
//        }];
        
    }
    else
    {
        UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整数据并查看密码是否正确" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil];
        [alert show];
    }

    
}

#pragma mark - textField代理
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.text_nextpassword resignFirstResponder];
    [self.text_password resignFirstResponder];
    [self.text_captcha resignFirstResponder];
    [self.text_tel resignFirstResponder];
}







@end
